<?php
/**
 * Shop_Model_Attr
 * 
 * @version $Id$
 * @author $Author$
 */
class Shop_Model_Attr extends Promotor_Model_Scaffold {

	protected $_dbTableClass = 'Shop_Model_DbTable_Attr';
	
	protected $_cachedMethods = array(
		'findByProductId',
		'findByProductAndAttrId'
	);
	
	/**
	 * Pobieranie zestawu atrybutów dla okreslonego produktu,
	 * 
	 * @param integer $productId
	 * @return array
	 */
	public function findByProductId($productId) {
		$db = $this->getDbTable()
				   ->getAdapter();

		$select = new Zend_Db_Select($db);

		$select->from(array('pv' => 'shop_product_attr_val'))
			   //->from(array('s' => 'shop_product_attrs_set'), array('attr_id'))
			   ->join(
			   		array('pa' => 'shop_product_attr'),
			   			'pa.id = pv.attr_id', array('name','type','required','default','description'))

			   	->where('pv.product_id = ?', $productId, Zend_Db::INT_TYPE);

		$result = array();

		try {
			$stmt = $select->query();
			$result = $stmt->fetchAll();
		} catch(Zend_Db_Exception $e) {
			$this->_setStatus(self::FAILURE);
			$this->_addException($e);
		}

		return $result;
	}
	
	/**
	 * Pobieranie zestawu atrybutów dla okreslonego produktu,
	 * 
	 * @param integer $productId
	 * @param integer $attrId
	 * @return array
	 */
	public function findByProductAndAttrId($productId, $attrId) {
		$db = $this->getDbTable()
				   ->getAdapter();

		$select = new Zend_Db_Select($db);

		$select->from(array('pv' => 'shop_product_attr_val'))
			   //->from(array('s' => 'shop_product_attrs_set'), array('attr_id'))
			   ->join(
			   		array('pa' => 'shop_product_attr'),
			   			'pa.id = pv.attr_id', array('name','type','required','default','description'))

			   	->where('pv.product_id = ?', $productId, Zend_Db::INT_TYPE)
			   	->where('pv.product_id = ?', $attrId, Zend_Db::INT_TYPE);

		$result = array();

		try {
			$stmt = $select->query();
			$result = $stmt->fetchAll();
		} catch(Zend_Db_Exception $e) {
			$this->_setStatus(self::FAILURE);
			$this->_addException($e);
		}

		return $result;
	}
}